package com.one.monitoring.task;

import com.one.monitoring.service.IALiYunSmsService;
import com.one.monitoring.service.IAppHttpApi;
import com.one.monitoring.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lxh
 * @description 监听定时任务
 * @time 2023/11/17 12:15
 **/
@EnableScheduling
@Slf4j
@Configuration
public class MonitoringTask {
    /**
     * 手机号
     */
    public static List<String> phoneList = Stream.of(
            "17691279016","15829549046"
    ).collect(Collectors.toList());
    @Autowired
    private IALiYunSmsService iaLiYunSmsService;
    @Autowired
    private IAppHttpApi appHttpApi;
    @Scheduled(cron = "0 0/15 * * * ?")//每两分钟执行一次
    public void getData2Save(){
        log.info("定时任务执行");
        List<Integer> list = new ArrayList<>();
        //1.mysql
        Integer mysql = MysqlTestUtils.testMysql();
        list.add(mysql);
        //2.redis
        Integer redis = RedisTestUtils.testRedis();
        list.add(redis);
        //3.mq
        Integer mq = MQTestUtils.testMQ();
        list.add(mq);
        //4.mongodb
        Integer mongodb = MongoDBTestUtils.testMongoDB();
        list.add(mongodb);
        //5.minio
        Integer minio = MinioTestUtils.testMinio();
        list.add(minio);
        //6.app服务
        Integer app = getApp();
        list.add(app);
        log.info("汇总检查结果：{}",list);
        //发送短信通知
        List<Integer> collect = list.stream().filter(item -> item > 1).collect(Collectors.toList());
        if (collect.size() > 0){
            for (String phone: phoneList){
            iaLiYunSmsService.SendSmsCode(phone, "110110");
            }
        }

    }

    private Integer getApp(){
        try {
            Map<String, Object> agreement = appHttpApi.agreement();
            Object code = agreement.get("code");
            if (code.equals("00000")){
                return 1;
            }
        }catch (Exception e){
            e.getMessage();
        }
        return 2;
    }
}
